package com.icss.ah.login.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.login.bean.UserInfoBean;
/**
 * 用户菜单权限
 * @author csadchen
 * @date 2017-03-23
 */
@Service
@Transactional
public class MenuRightService {

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	/**
	 * 获取用户菜单权限
	 * @param hm
	 * @return
	 */
	public List queryMenuList(HashMap hm){
		List menuList = null;
		try{
			//根据登录账号获取账号基本信息
			String shopId = hm.get("shopId") == null ? "" : hm.get("shopId").toString();
			String applyStatus = hm.get("applyStatus") == null ? "0" : hm.get("applyStatus").toString();
			String registerStatus = hm.get("registerStatus") == null ? "0" : hm.get("registerStatus").toString();
			String isOwner = hm.get("isOwner") == null ? "0" : hm.get("isOwner").toString();
			String menuType = hm.get("menuType") == null ? "menuHead" : hm.get("menuType").toString();
			String ifMain = hm.get("ifMain") == null ? "0" : hm.get("ifMain").toString();
			
			if(shopId.equals("")||applyStatus.equals("0")||(applyStatus.equals("1")&&!registerStatus.equals("1"))){
				if(menuType.equals("menuHead")){
					menuList = sqlSessionTemplate.selectList("MenuRightBean.queryNoShopMenuList", hm);
				} else {
					menuList = sqlSessionTemplate.selectList("MenuRightBean.queryNoShopMenuList", hm);
				}
			} else {
				if(isOwner.equals("1")){
					//获取店主菜单权限
					if(menuType.equals("menuHead")){
						//首页获取模块菜单
						menuList = sqlSessionTemplate.selectList("MenuRightBean.queryShopMenuList", hm);
					} else {
						if(ifMain.equals("1")){
							//首页获取快捷菜单
							menuList = sqlSessionTemplate.selectList("MenuRightBean.queryShopMainMenuList", hm);
						} else {
							//非首页获取模块菜单
							menuList = sqlSessionTemplate.selectList("MenuRightBean.queryShopMenuList", hm);
						}
					}
				} else {
					//获取店员菜单权限
					if(menuType.equals("menuHead")){
						//首页获取模块菜单
						menuList = sqlSessionTemplate.selectList("MenuRightBean.queryShopUserMenuList", hm);
					} else {
						if(ifMain.equals("1")){
							//首页获取快捷菜单
							menuList = sqlSessionTemplate.selectList("MenuRightBean.queryShopMainMenuList", hm);
						} else {
							//非首页获取模块菜单
							menuList = sqlSessionTemplate.selectList("MenuRightBean.queryShopUserMenuList", hm);
						}
					}
					
				}
			}
			infolog.info("MenuRightService|queryMenuList|menuList:"+menuList.size());
			menuList = menuList == null ? new ArrayList() : menuList;
		} catch (Exception e) {
			e.printStackTrace();
			menuList = new ArrayList();
			errlog.error("MenuRightService|queryMenuList", e);
		}
		return menuList;
	}
	/**
	 * 设为快捷菜单
	 * @param hm
	 * @return
	 */
	public String setShortcut(HashMap hm){
		String result = "0, ";
		try{
			//判断是否有现有记录
			Integer count = sqlSessionTemplate.selectOne("MenuRightBean.queryUserShortcutCount", hm);
			if(count>0){
				//有则修改
//				sqlSessionTemplate.selectOne("MenuRightBean.updateUserShortcut", hm);
			} else {
				//无则新增
				hm.put("uuid", UUID.randomUUID().toString());
				Integer shortcutOrder = sqlSessionTemplate.selectOne("MenuRightBean.queryUserShortcutOrder", hm);
				shortcutOrder = shortcutOrder == null ? 0 : shortcutOrder;
				shortcutOrder = shortcutOrder+1;
				hm.put("shortcutOrder", shortcutOrder);
				sqlSessionTemplate.selectOne("MenuRightBean.insertUserShortcut", hm);
			}
			result = "1,success";
		} catch (Exception e) {
			e.printStackTrace();
			result = "2,"+e.getMessage();
		}
		return result;
	}
}
